package com.ccn.kotlin_mvvm.app

import androidx.multidex.MultiDex
import cat.ereza.customactivityoncrash.config.CaocConfig
import com.ccn.jetpack_mvvm.base.BaseApp
import com.ccn.jetpack_mvvm.ext.util.jetpackMvvmLog
import com.ccn.kotlin_mvvm.BuildConfig
import com.ccn.kotlin_mvvm.app.event.AppViewModel
import com.ccn.kotlin_mvvm.app.event.EventViewModel
import com.ccn.kotlin_mvvm.app.wiget.loadCallBack.EmptyCallBack
import com.ccn.kotlin_mvvm.app.wiget.loadCallBack.ErrorCallback
import com.ccn.kotlin_mvvm.app.wiget.loadCallBack.LoadingCallBack
import com.ccn.kotlin_mvvm.ui.ErrorActivity
import com.ccn.kotlin_mvvm.ui.WelcomeActivity
import com.kingja.loadsir.callback.SuccessCallback
import com.kingja.loadsir.core.LoadSir
import com.tencent.mmkv.MMKV

//application全局的ViewModel，里面存放了一些账户信息，基本配置信息等
val appViewModel:AppViewModel by lazy { App.appViewModelInstance }
//application全局的ViewModel，用于发送全局通知操作
val eventViewModel:EventViewModel by lazy { App.eventViewModelInstance }

class App : BaseApp() {
    companion object {
        lateinit var instance: App
        lateinit var eventViewModelInstance: EventViewModel
        lateinit var appViewModelInstance: AppViewModel
    }

    override fun onCreate() {
        super.onCreate()
        MMKV.initialize(this.filesDir.absolutePath+"/mmkv")
        instance = this
        eventViewModelInstance = getAppViewModelProvider().get(EventViewModel::class.java)
        appViewModelInstance = getAppViewModelProvider().get(AppViewModel::class.java)
        MultiDex.install(this)
        //界面加载管理  初始化
        LoadSir.beginBuilder()
            .addCallback(LoadingCallBack())
            .addCallback(ErrorCallback())
            .addCallback(EmptyCallBack())
            .setDefaultCallback(SuccessCallback::class.java)
            .commit()

        //初始化bugly略
        jetpackMvvmLog = BuildConfig.DEBUG

        //防止项目崩溃，崩溃后打开错误页面
        CaocConfig.Builder.create()
            .backgroundMode(CaocConfig.BACKGROUND_MODE_SILENT)
            .enabled(true)
            .showErrorDetails(false)
            .showRestartButton(false)
            .logErrorOnRestart(false)
            .trackActivities(true)
            .minTimeBetweenCrashesMs(2000)
            .restartActivity(WelcomeActivity::class.java)
            .errorActivity(ErrorActivity::class.java)
            .apply()

    }

}